//
//  GQDistributeToolPhotoUrl.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/15.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQDistributeToolPhotoUrl.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "GQDistributePhotoInfos.h"
#import "GQDistributPhotoInfo.h"

@interface GQDistributeToolPhotoUrl ()

@property (strong, nonatomic) NSMutableArray *dataUrlArrM;
@property (strong, nonatomic) NSMutableArray *groups;

@end

@implementation GQDistributeToolPhotoUrl
singleton_implementation(GQDistributeToolPhotoUrl)
- (instancetype)init
{
    if (self = [super init]) {
        [self getImages];
        [self performSelector:@selector(getInfos) withObject:nil afterDelay:2.0];
    }
    return self;
}

- (void)getInfos;
{
    GQDistributePhotoInfos *GQDOI = [GQDistributePhotoInfos sharedGQDistributePhotoInfos];
    [GQDOI savePhotoInfos:self.dataUrlArrM];

}

- (void)getImages
{
    __weak GQDistributeToolPhotoUrl *weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *myError){
            NSLog(@"相册访问失败 = %@", [myError localizedDescription]);
            if ([myError.localizedDescription rangeOfString:@"Global denied access"].location != NSNotFound) {
                NSLog(@"无法访问相册。请在'设置->定位服务'设置为打开状态。");
            }else{
                NSLog(@"相册访问失败！");
            }
        };
        
//        ALAssetsGroupEnumerationResultsBlock groupEnumerAction = ^(ALAsset *result, NSUInteger index, BOOL *stop){
//            if (result != NULL) {
//                if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
//                    NSString *urlStr = [NSString stringWithFormat:@"%@", result.defaultRepresentation.url];//图片的url
//                    [weakSelf.dataUrlArrM addObject:urlStr];
//                }
//            }
//        };
        
        ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup *group, BOOL *stop){
            if(group != nil){
                [weakSelf.groups addObject:group];
            }else{
                [weakSelf.groups enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    [obj enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                        if ([result thumbnail] != nil) {
                            //照片
                            if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                                GQDistributPhotoInfo *GQDP = [[GQDistributPhotoInfo alloc] init];
                                GQDP.createDate = [result valueForProperty:ALAssetPropertyDate];
                                GQDP.image = [UIImage imageWithCGImage:[result thumbnail]];
                                GQDP.fileName = [[result defaultRepresentation] filename];
                                GQDP.originalUrl = [[result defaultRepresentation] url];
                                GQDP.fileSize = [[result defaultRepresentation] size];
                                [self.dataUrlArrM addObject:GQDP];
                            }
                        }
                    }];
                }];
            }
        };
        
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:libraryGroupsEnumeration failureBlock:failureBlock];
        
    });
    
}


- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (NSMutableArray *)dataUrlArrM
{
    if (_dataUrlArrM == nil) {
        _dataUrlArrM = [NSMutableArray array];
    }
    return _dataUrlArrM;
}


@end
